package com.pureut.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.order.domain.SysSupplierCoding;
import com.pureut.order.mapper.SysSupplierCodingMapper;
import com.pureut.order.service.ISysSupplierCodingService;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.core.utils.StringUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: YSL
 * @Date: 2023/7/10 17:44
 * @Desc: 供应商类别
 */
@RequiredArgsConstructor
@Service
public class SysSupplierCodingServiceImpl implements ISysSupplierCodingService {

    /**
     * 供应商类别Mapper
     */
    private final SysSupplierCodingMapper sysSupplierCodingMapper;

    /**
     * 分页查询供应商类别列表
     *
     * @param supplierCoding
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<SysSupplierCoding> selectSupplierCodingList(SysSupplierCoding supplierCoding, PageQuery pageQuery) {
        // 模糊查询
        LambdaQueryWrapper<SysSupplierCoding> lambdaQueryWrapper = new LambdaQueryWrapper<SysSupplierCoding>()
            .like(StringUtils.isNotBlank(supplierCoding.getSupplierRules()), SysSupplierCoding::getSupplierRules, supplierCoding.getSupplierRules())
            .like(StringUtils.isNotBlank(supplierCoding.getSupplierCode()), SysSupplierCoding::getSupplierCode, supplierCoding.getSupplierCode())
            .orderByDesc(SysSupplierCoding::getCreateTime);
        // 分页
        Page<SysSupplierCoding> sysSupplierCodingPage = sysSupplierCodingMapper.selectPage(pageQuery.build(), lambdaQueryWrapper);

        return TableDataInfo.build(sysSupplierCodingPage);
    }

    /**
     * 查询单个供应商类别
     *
     * @param supplierId
     * @return
     */
    @Override
    public SysSupplierCoding getSupplierCodingById(Long supplierId) {
        return sysSupplierCodingMapper.selectById(supplierId);
    }

    /**
     * 新增供应商类别
     *
     * @param supplierCoding
     * @return
     */
    @Override
    public int insertSupplierCoding(SysSupplierCoding supplierCoding) {
        //校验该编码是否已经存在
        SysSupplierCoding sysSupplierCoding = sysSupplierCodingMapper.selectOne(new QueryWrapper<SysSupplierCoding>().lambda()
            .eq(SysSupplierCoding::getSupplierCode, supplierCoding.getSupplierCode()));
        if (sysSupplierCoding != null) {
            throw new GlobalException("该编码已经存在");
        }
        supplierCoding.setCreateTime(new Date());
        return sysSupplierCodingMapper.insert(supplierCoding);
    }

    /**
     * 修改供应商类别
     *
     * @param supplierCoding
     * @return
     */
    @Override
    public int updateSupplierCoding(SysSupplierCoding supplierCoding) {
        return sysSupplierCodingMapper.updateById(supplierCoding);
    }

    /**
     * 删除供应商类别
     *
     * @param idStr
     * @return
     */
    @Override
    public boolean deleteSupplierCoding(String idStr) {
        if(idStr == null){
            throw new GlobalException("至少选择一条数据");
        }
        List<String> idList = Arrays.asList(idStr.split(","));
        return sysSupplierCodingMapper.deleteByIds(idList) > 0;
    }

    /**
     * 获取供应商类别下拉列表
     *
     * @return
     */
    @Override
    public List<SysSupplierCoding> downList() {
        return sysSupplierCodingMapper.selectList();
    }
}
